﻿using Ducu.SRV.ServiceInteface;
using Ducu.SRV.ViewModel.Password;
using Ducu.UI.Responsible.Filter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Ducu.UI.Responsible.Controllers
{
    public class PasswordController : BaseController
    {
        #region Constructor

        private IPasswordService _pwdService;
        public PasswordController(IPasswordService pwdService)
        {
            _pwdService = pwdService;
        }

        #endregion

        #region URL: /Password/Reset

        public ActionResult Reset()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Reset(int id, ResetModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            _pwdService.Reset(id, model.Password);

            return RedirectToAction("On","Log");
        }
    
        #endregion

        #region URL: /Password/Change

        [NeedLogOn]
        public ActionResult Change()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Change(ChangModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            if (!_pwdService.IsCorrect(model.Old))
            {
                ModelState.AddModelError("Old", "* 旧密码错误");
                return View(model);
            }

            _pwdService.Change(model.New);

            return Prepage.Return();
        }

        #endregion

    }
}